package org.bigk.invoices.actions.ajax;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.bigk.invoices.actions.InvoicesActionSupport;
import org.bigk.invoices.model.Invoice;
import org.bigk.invoices.services.PaymentKindsService;

public class PaymentDateAction extends InvoicesActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory
			.getLog(PaymentDateAction.class);

	private static final long serialVersionUID = 1558037045535040777L;
	
	private Invoice model;
	
	protected PaymentKindsService paymentKindsService;
	
	@Override
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - model=" + model);
		}

		Long pkid = model.getPaymentKindId();
		if (pkid != null) {
			Date paymentDate = paymentKindsService.calculatePaymentDate(model.getSoldDate(), pkid);
			model.setPaymentDate(paymentDate);
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end - return value=" + SUCCESS);
		}
		return SUCCESS;
	}

	public Invoice getModel() {
		return model;
	}

	public void setModel(Invoice model) {
		this.model = model;
	}

	public PaymentKindsService getPaymentKindsService() {
		return paymentKindsService;
	}

	public void setPaymentKindsService(PaymentKindsService paymentKindsService) {
		this.paymentKindsService = paymentKindsService;
	}
	
}
